import { showToast } from '../utils/common.js'
import useUserStore from '@/store/modules/user.js'


// const BASE_URL = "https://wmtest.mtj.cdydsy.com/user"
const BASE_URL = "http://192.168.10.66:8080/user"

// export const UPLOAD_BASE_URL = "https://wmtest.mtj.cdydsy.com/user/file/upload"

// const BASE_URL = "http://192.168.10.81:8080/user"
export const UPLOAD_BASE_URL = "http://192.168.10.81:8080/user/file/upload"

export function request(config = {}) {
	let { url, method = "GET", header = {}, data = {} } = config
	url = BASE_URL + url
	// header["access-key"] = "lc123456"
	// header["token"] = "12312312"
	header = getHeaders()
	return new Promise((resolve, reject) => {
		uni.request({
			url,
			data,
			method,
			header,
			success: res => {
				if (res.data.code === 200) {
					resolve(res.data)
				} else if (res.data.code === 50000) {
					showToast(res.data.msg);
					reject(res.data)
				} else if (res.data.code === 404) {
					showToast("当前接口不存在");
					reject(res.data)
				} else if (res.data.code === 203) {
					// showToast(res.data.msg);
					const userStore = useUserStore()
					userStore.clearLoginInfo()
					return reject(new Error('身份验证会话已过期，请重新登录!'));
				} else if (res.data.code === 0) {
					reject(res.data)
				} else {
					uni.showModal({
						title: res.data.msg || "网络异常！",
						icon: 'none'
					})
					reject(res.data)
				}
			},
			fail: err => {
				reject(err)
			}
		})
	})
}

/**
 * @desc 请求头数据
 */
function getHeaders(e) {
	let header = {
		"Authorization": `Bearer ${uni.getStorageSync('token')}` || "",
	}
	return header
}